<!-- src/views/Callback.vue -->
<template>
  <div class="callback-container">
    <div v-if="loading" class="loading">
      <p>正在处理登录信息...</p>
    </div>
    <div v-else-if="error" class="error">
      <p>登录失败: {{ error }}</p>
      <button @click="$router.push('/login')">重新登录</button>
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import authService from '/@/api/oidcClient/authService'

const router = useRouter()
const route = useRoute()
const loading = ref(true)
const error = ref('')

onMounted(async () => {
  try {
    const { code, state } = route.query
    
    if (!code) {
      throw new Error('授权码缺失')
    }
    await authService.handleCallback(code, state)
    router.push('/')
  } catch (err) {
    error.value = err.message
  } finally {
    loading.value = false
  }
})
</script>

<style scoped>
.callback-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
}

.loading, .error {
  text-align: center;
}

.error button {
  margin-top: 1rem;
  padding: 8px 16px;
  background-color: #007bff;
  color: white;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}
</style>